<?php
/**
 * Created by PhpStorm.
 * User: liuxiaojie
 * Date: 2019-03-07
 * Time: 11:14
 */

namespace app\service;

use app\tools\MIniprogram;

class PromoService extends BaseService{

    static function createPromoCode($num = 1){

        $base = db('promo_code')->max('base_num');

        $post = [];

        foreach (range($base + 1, $base + $num) as $v){

            $rand = rand(10, 99);

            $code = base_convert($v, 10, 36).$rand;
            $code = self::fill($code);
//            $img = MIniprogram::createCodeImg2($code);
            $img = MIniprogram::createCodeImg($code);
            $post[] = [
                "img"=> $img,
                "code"=> $code,
                "base_num"=> $v,
            ];

        }

        db('promo_code')->insertAll($post);


    }

    static function fill($code, $length = 6){

        $num = $length - strlen($code);
        return strval(str_repeat("0", $num).$code);

    }


}